<?php

class SquadController extends Zend_Controller_Action {

    public function init() {
        // return default view
    }

    // [GET]
    public function indexAction() {
        if(SessionServices::HasSessionStarted()) {
            $this->_redirector = $this->_helper->getHelper('Redirector');
            $this->_redirector->gotoUrl('/dashboard');
        }
    }

    // [GET]
    public function newAction() {
        // return default view
    }

    // [POST]
    public function newdoAction() {
        if($this->getRequest()->isPost()) {
            $squadName = $this->_getParam('txtSquadName');
            $squad = new Squad();
            $squad->AddNewSquad($squadName, SessionServices::GetSessionId());
        }

        return $this->_helper->redirector('index');
    }

    // [GET]
    public function transfers() {

    }

    // [GET]
    public function formationAction() {
        $squad_id = $this->_getParam('id');
        
        $this->view->error = $this->_getParam('error');

        if(!$this->getRequest()->isGet() || !isset ($squad_id))
            return $this->_helper->redirector('index');

        $this->view->squad_id = $squad_id;
        if(isset($squad_id)) {
            $formations = SquadServices::GetFormation($squad_id);
            $places[] = 11;
            if($formations) {
                $this->view->hasFormation = TRUE;
                $this->view->formation = $formations;
            }
        }

    }

    // [GET]
    public function formationdoAction() {
        $player_id = $this->_getParam('player_id');
        $squad_id = $this->_getParam('squad_id');

        if(!$this->getRequest()->isGet() ||
                (!isset ($player_id) && !isset($squad_id)))
            return $this->_helper->redirector('index');

        $this->_redirector = $this->_helper->getHelper('Redirector');

        $player = PlayerServices::GetPlayerById($player_id);

        $squadObj = new Squad();
        $squad = $squadObj->GetSquadById($squad_id);

        if($player && $squad) {
            $formations = SquadServices::GetFormation($squad_id);

            $allow = FALSE;
            $found = FALSE;

            if(!$formations)
                $allow = TRUE;

            $position_counter = 0;
            if(!$allow) {
                foreach($formations as $form) {
                    if($form->position_id == $player->position_id)
                        $position_counter = $position_counter + 1;

                    if($form->player_id == $player->id)
                        $found = TRUE;
                }
                if(!$found) {
                    $formation_type = SquadServices::GetFormationType($squad->formation_type_id);
                    switch($player->position_id) {
                        case 1:
                            $allow = ($position_counter == 1) ? TRUE : FALSE;
                            break;
                        case 2:
                            $allow = ($position_counter < $formation_type->defenders) ? TRUE : FALSE;
                            break;
                        case 3:
                            $allow = ($position_counter < $formation_type->midfielders) ? TRUE : FALSE;
                            break;
                        case 4:
                            $allow = ($position_counter < $formation_type->strikers) ? TRUE : FALSE;
                            break;
                    }
                }
            }

            if($allow) {
                // write to db
                // instantiated above
                $formation->AddPlayer($player_id, $player->position_id, $squad_id);
                $this->_redirector->gotoUrl('/squad/formation/id/'.$squad_id);
            } else {
                if($found)
                    $this->_redirector->gotoUrl('/squad/formation/error/player_already_chosen');
                else
                    $this->_redirector->gotoUrl('/squad/formation/error/limit_reached');
            }

        } else {
            $this->_redirector->gotoUrl('/squad/formation/error/invalid_ids');
        }

    }



}

?>
